package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.basic.Agent;
import cc.yugu.yhdw.common.domain.hdw.AgencyInfo;
import cc.yugu.yhdw.common.domain.hdw.Stats;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * 报表业务
 * 
 * @Date:Created in 2018/7/10 16:50
 * @Author:
 * @Modified By: zjn 2018/7/10 16:50
 */ 
@Service
public class ReportService extends cc.yugu.yhdw.webserver.service.AbstractService {
    @Autowired
    AgentDayStatsMapper agentDayStatsMapper;
    @Autowired
    AgentMonthStatsMapper agentMonthStatsMapper;
    @Autowired
    AgencyDayStatsMapper agencyDayStatsMapper;
    @Autowired
    AgencyMonthStatsMapper agencyMonthStatsMapper;
    @Autowired
    CabinetDayStatsMapper cabinetDayStatsMapper;
    @Autowired
    CabinetMonthStatsMapper cabinetMonthStatsMapper;
    @Autowired
    AgentMapper agentMapper;
    /**
    * 分页查询报表
    * 
    * @Param: [stats]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/10  16:50
    */ 
    public Page findPagePaid(Stats stats) {
        Page page = stats.buildPage();
        stats.setBeginIndex(page.getOffset());
        List<Stats> list = new ArrayList<Stats>();
        switch (stats.getFlag()) {
            case 1:
                page.setTotalItems(agentDayStatsMapper.findPagePaidCount(stats));
                list = agentDayStatsMapper.findPagePaidResult(stats);
                break;
            case 2:
                page.setTotalItems(agentMonthStatsMapper.findPagePaidCount(stats));
                list = agentMonthStatsMapper.findPagePaidResult(stats);
                break;
            case 3:
                page.setTotalItems(agencyDayStatsMapper.findPagePaidCount(stats));
                list = agencyDayStatsMapper.findPagePaidResult(stats);
                break;
            case 4:
                page.setTotalItems(agencyMonthStatsMapper.findPagePaidCount(stats));
                list = agencyMonthStatsMapper.findPagePaidResult(stats);
                break;
            case 5:
                page.setTotalItems(cabinetDayStatsMapper.findPagePaidCount(stats));
                list = cabinetDayStatsMapper.findPagePaidResult(stats);
                break;
            case 6:
                page.setTotalItems(cabinetMonthStatsMapper.findPagePaidCount(stats));
                list = cabinetMonthStatsMapper.findPagePaidResult(stats);
                break;
        }

        for (Stats s : list) {
            if (s.getAgentId() != null) {
                Agent agent = agentMapper.find(s.getAgentId());
                s.setAgentName(agent == null ? "" : agent.getAgentName());
            }
            if (s.getAgencyId() != null) {
                AgencyInfo agency = findAgency(s.getAgencyId());
                s.setAgencyName(agency == null ? "" : agency.getAgencyName());
            }
        }
        page.setResult(list);
        return page;
    }
    /**
    * 柜子每日统计：金额、订单数、更新时间
    * 
    * @Param: [statsDate, agentId, agencyId]
    * @Return: java.util.Map<java.lang.String,java.lang.Object>
    * @Author By: zjn 2018/7/10  16:50
    */ 
    public Map<String,Object> findToDayStats(String statsDate,Integer agentId,Integer agencyId){
        return cabinetDayStatsMapper.findToDayStats(statsDate,agentId,agencyId);
    }
    /**
    * 新增：运营商每日信息统计
    * 
    * @Param: [statsDate, agentId]
    * @Return: java.util.Map<java.lang.String,java.lang.Object>
    * @Author By: zjn 2018/7/18  16:18
    */ 
    public Map<String,Object> findAgentToDayStats(String statsDate,Integer agentId){
        return cabinetDayStatsMapper.findAgentToDayStats(statsDate,agentId);
    }
}
